package com.inossem.warehousestorage_inossem.activity.analysis;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.utils.base.Utils;
import com.inossem.utils.chart.ChartActivity;
import com.inossem.utils.chart.ChartBean;
import com.inossem.utils.chart.ChartUtils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.analysis.extra.AnalysisExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsChartBean;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsGetPersonBean;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsIndexBean;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsRequestBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.analysis.AnalysisWorkStatisticsGetChartRequest;
import com.inossem.warehousestorage_inossem.request.analysis.AnalysisWorkStatisticsGetPersonRequest;
import com.inossem.warehousestorage_inossem.request.analysis.AnalysisWorkStatisticsIndexRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AnalysisUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.library.smoothCheckBox.SmoothCheckBox;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class AnalysisWorkStatisticsIndexActivity extends BaseActivity {

    private TextView title, block, factory, inventoryAddress, person, beginDate, endDate, nextStep;

    private TextView blockValue, factoryValue, inventoryAddressValue, personValue;

    private PercentRelativeLayout blockHide, factoryHide, inventoryAddressHide, dateHide;

    private SmoothCheckBox date;

    private ImageView findPerson;

    private String mIsFocus;

    private AnalysisWorkStatisticsIndexBean mBean;

    private List<AnalysisWorkStatisticsGetPersonBean.DataBean.PersonListBean> mPersonList;

    public static final int FIND_PERSON = 100;

    private static final long ONE_DAY = 1000L * 60L * 60L * 24L;

    @Override

    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_analysis_work_statistics_index);
        initView(AnalysisWorkStatisticsIndexActivity.this);
        initData(AnalysisWorkStatisticsIndexActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        block = (TextView) activity.findViewById(R.id.block);
        factory = (TextView) activity.findViewById(R.id.factory);
        inventoryAddress = (TextView) activity.findViewById(R.id.inventoryAddress);
        person = (TextView) activity.findViewById(R.id.person);
        beginDate = (TextView) activity.findViewById(R.id.beginDate);
        endDate = (TextView) activity.findViewById(R.id.endDate);

        blockValue = (TextView) activity.findViewById(R.id.blockValue);
        factoryValue = (TextView) activity.findViewById(R.id.factoryValue);
        inventoryAddressValue = (TextView) activity.findViewById(R.id.inventoryAddressValue);
        personValue = (TextView) activity.findViewById(R.id.personValue);

        blockHide = (PercentRelativeLayout) activity.findViewById(R.id.blockHide);
        factoryHide = (PercentRelativeLayout) activity.findViewById(R.id.factoryHide);
        inventoryAddressHide = (PercentRelativeLayout) activity.findViewById(R.id.inventoryAddressHide);
        dateHide = (PercentRelativeLayout) activity.findViewById(R.id.dateHide);

        date = (SmoothCheckBox) activity.findViewById(R.id.date);
        findPerson = (ImageView) activity.findViewById(R.id.findPerson);

        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_analysis_manager_work_statistics);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_analysis_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void showHideLayout(String blockValue, String factoryValue, String inventoryAddressValue) {
        blockHide.setVisibility(View.GONE);
        factoryHide.setVisibility(View.GONE);
        inventoryAddressHide.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(blockValue)) {
            blockHide.setVisibility(View.VISIBLE);
        } else {
            return;
        }

        if (!TextUtils.isEmpty(factoryValue)) {
            factoryHide.setVisibility(View.VISIBLE);
        } else {
            return;
        }

        if (!TextUtils.isEmpty(inventoryAddressValue)) {
            inventoryAddressHide.setVisibility(View.VISIBLE);
        } else {
            return;
        }
    }

    private void getData(Context context) {
        date.setChecked(true, false);

        findPerson.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, AnalysisWorkStatisticsFindPersonActivity.class);
                startActivityForResult(intent, FIND_PERSON);
            }
        });

        person.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (checkPersonCondition(context)) {
                    DialogUtils.getWorkStatisticsPersonDialog(context, mPersonList, new DialogUtils.AnalysisWorkStatisticsListener() {
                        @Override
                        public void onSelect(Dialog dialog, String text, String value) {
                            person.setText(text);
                            personValue.setText(value);
                        }
                    }).show();
                }
            }
        });

        beginDate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ActivityUtils.getTimePickerDialogCustom(context, Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, beginDate.getText().toString()), null, Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, endDate.getText().toString()) - ONE_DAY, new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        beginDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                    }
                }).show(getSupportFragmentManager(), "beginDate");
            }
        });

        endDate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ActivityUtils.getTimePickerDialogCustom(context, Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, endDate.getText().toString()), Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, beginDate.getText().toString()) + ONE_DAY, null, new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        endDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                    }
                }).show(getSupportFragmentManager(), "endDate");
            }
        });

        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                AnalysisWorkStatisticsRequestBean params = AnalysisUtils.getAnalysisWorkStatisticsRequestBean(context, blockValue.getText().toString(), factoryValue.getText().toString(), inventoryAddressValue.getText().toString(), personValue.getText().toString(), beginDate.getText().toString(), endDate.getText().toString());
                AnalysisWorkStatisticsGetChartRequest.request(context, params, new GsonBeanListener<AnalysisWorkStatisticsChartBean>() {
                    @Override
                    public void onSuccess(AnalysisWorkStatisticsChartBean bean) {
                        Gson gson = new Gson();
                        String chartJson = gson.toJson(bean.getData(), ChartBean.class);
                        Intent intent = new Intent(context, ChartActivity.class);
                        intent.putExtra(ChartUtils.KEY_INFORMATION, chartJson);
                        startActivity(intent);
                    }
                });
            }
        });
        AnalysisWorkStatisticsIndexRequest.request(context, new GsonBeanListener<AnalysisWorkStatisticsIndexBean>() {
            @Override
            public void onSuccess(AnalysisWorkStatisticsIndexBean bean) {
                mBean = bean;
                block.setText(mBean.getData().getBlockName());
                blockValue.setText(mBean.getData().getBlockValue());
                factory.setText(mBean.getData().getFactoryName());
                factoryValue.setText(mBean.getData().getFactoryValue());
                inventoryAddress.setText(mBean.getData().getInventoryAddressName());
                inventoryAddressValue.setText(mBean.getData().getInventoryAddressValue());
                if (!TextUtils.isEmpty(inventoryAddressValue.getText().toString()) && !TextUtils.isEmpty(factoryValue.getText().toString())) {
                    AnalysisWorkStatisticsGetPersonRequest.request(context, factoryValue.getText().toString(), inventoryAddressValue.getText().toString(), new GsonBeanListener<AnalysisWorkStatisticsGetPersonBean>() {
                        @Override
                        public void onSuccess(AnalysisWorkStatisticsGetPersonBean bean) {
                            mPersonList = AnalysisUtils.getPersonList(bean.getData().getPersonList());
                            person.setText(mPersonList.get(0).getName());
                            personValue.setText(mPersonList.get(0).getId());
                        }
                    });
                }
                showHideLayout(blockValue.getText().toString(), factoryValue.getText().toString(), inventoryAddressValue.getText().toString());
                beginDate.setText(mBean.getData().getBeginTime());
                endDate.setText(mBean.getData().getEndTime());
                dateHide.setVisibility(View.VISIBLE);
                block.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        DialogUtils.getWorkStatisticsBlockDialog(context, mBean.getData().getBlockList(), new DialogUtils.AnalysisWorkStatisticsListener() {
                            @Override
                            public void onSelect(Dialog dialog, String text, String value) {
                                block.setText(text);
                                blockValue.setText(value);
                                factory.setText(AnalysisUtils.getFirstFactory().getFactoryName());
                                factoryValue.setText(AnalysisUtils.getFirstFactory().getFactoryValue());
                                showHideLayout(blockValue.getText().toString(), factoryValue.getText().toString(), inventoryAddressValue.getText().toString());
                            }
                        }).show();
                    }
                });

                factory.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        List<AnalysisWorkStatisticsIndexBean.DataBean.BlockListBean.FactoryListBean> factoryList = AnalysisUtils.getFactoryList(mBean, blockValue.getText().toString());
                        DialogUtils.getWorkStatisticsFactoryDialog(context, factoryList, new DialogUtils.AnalysisWorkStatisticsListener() {
                            @Override
                            public void onSelect(Dialog dialog, String text, String value) {
                                factory.setText(text);
                                factoryValue.setText(value);
                                inventoryAddress.setText(AnalysisUtils.getFirstInventoryAddress().getInventoryAddressName());
                                inventoryAddressValue.setText(AnalysisUtils.getFirstInventoryAddress().getInventoryAddressValue());
                                showHideLayout(blockValue.getText().toString(), factoryValue.getText().toString(), inventoryAddressValue.getText().toString());
                            }
                        }).show();
                    }
                });

                inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        List<AnalysisWorkStatisticsIndexBean.DataBean.BlockListBean.FactoryListBean.InventoryAddressListBean> inventoryAddressList = AnalysisUtils.getInventoryAddressList(mBean, blockValue.getText().toString(), factoryValue.getText().toString());
                        DialogUtils.getWorkStatisticsInventoryAddressDialog(context, inventoryAddressList, new DialogUtils.AnalysisWorkStatisticsListener() {
                            @Override
                            public void onSelect(Dialog dialog, String text, String value) {
                                inventoryAddress.setText(text);
                                inventoryAddressValue.setText(value);
                                showHideLayout(blockValue.getText().toString(), factoryValue.getText().toString(), inventoryAddressValue.getText().toString());
                                if (!TextUtils.isEmpty(inventoryAddressValue.getText().toString()) && !TextUtils.isEmpty(factoryValue.getText().toString())) {
                                    AnalysisWorkStatisticsGetPersonRequest.request(context, factoryValue.getText().toString(), inventoryAddressValue.getText().toString(), new GsonBeanListener<AnalysisWorkStatisticsGetPersonBean>() {
                                        @Override
                                        public void onSuccess(AnalysisWorkStatisticsGetPersonBean bean) {
                                            mPersonList = AnalysisUtils.getPersonList(bean.getData().getPersonList());
                                            person.setText(mPersonList.get(0).getName());
                                            personValue.setText(mPersonList.get(0).getId());
                                        }
                                    });
                                }
                            }
                        }).show();
                    }
                });

                date.setOnCheckedChangeListener((SmoothCheckBox checkBox, boolean isChecked) -> {
                    if (isChecked) {
                        dateHide.setVisibility(View.VISIBLE);
                        beginDate.setText(mBean.getData().getBeginTime());
                        endDate.setText(mBean.getData().getEndTime());
                    } else {
                        dateHide.setVisibility(View.GONE);
                        beginDate.setText("");
                        endDate.setText("");
                    }
                });

            }
        });

    }

    private boolean checkPersonCondition(Context context) {
        String blockValueStr = blockValue.getText().toString();
        if (TextUtils.isEmpty(blockValueStr)) {
            Utils.middleToast(context, context.getResources().getString(R.string.analysis_work_statistics_index_choose_block));
            return false;
        }

        String factoryValueStr = factoryValue.getText().toString();
        if (TextUtils.isEmpty(factoryValueStr)) {
            Utils.middleToast(context, context.getResources().getString(R.string.analysis_work_statistics_index_choose_factory));
            return false;
        }

        String inventoryAddressValueStr = inventoryAddressValue.getText().toString();
        if (TextUtils.isEmpty(inventoryAddressValueStr)) {
            Utils.middleToast(context, context.getResources().getString(R.string.analysis_work_statistics_index_choose_inventory_address));
            return false;
        }

        if (mPersonList == null || mPersonList.isEmpty()) {
            Utils.middleToast(context, context.getResources().getString(R.string.analysis_work_statistics_index_person_is_empty));
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (requestCode) {
                case FIND_PERSON:
                    person.setText(data.getStringExtra(AnalysisExtra.PERSON_NAME));
                    personValue.setText(data.getStringExtra(AnalysisExtra.PERSON_ID));
                    break;
            }
        }
    }

}
